<?php

require_once '../conexion.php';

class listUser {

    private $conx;
    private $table;
    private $race = "SELECT * FROM RACE WHERE racecod <=6";
    private $food = "SELECT * FROM FOOD";
    private $bacth = "SELECT b.batchcod, b.batchname, b.landcod,l.landname, l.landcod 
                      FROM batch as b,land as l 
                      WHERE  l.userid='%s' and b.landcod = l.landcod";
    private $cattle = "  SELECT * FROM cattle  WHERE batchcod = '%s'";
    private $cattle2 = "SELECT * FROM cattle WHERE batchcod = '%s' and dateregister ='%s'";
    private $prom = "select f.foodcod, f.namefood, r.racecod, r.namerace, b.racebatch, b.batchcod, b.batchname, b.foodbatch, b.dateregisterb, 
                    gu.batchcod, gu.growthdate, gu.weightbirth, gu.weightpreweaning, gu.weightweaning, gu.weightpreceba, gu.weightceba, gu.agepreweaning, gu.ageweaning, gu.agepreceba, gu.ageceba
                    from food as f, race as r, batch as b, growth_user as gu where b.batchcod = gu.batchcod and r.racecod = b.racebatch and f.foodcod=b.foodbatch";
    private $disease= "SELECT * FROM disease ='%s'";
    private $foodcod = "SELECT foodbatch FROM batch WHERE batchcod='%s'";
    private $fc;


    public function __construct() {
        $this->conx = null;
    }

    public function listRaces() {
        $consult = sprintf($this->race);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        if (!$result) {
            echo 'Error listando las Razas \n';
        } while ($rows = pg_fetch_row($result)) {
            echo "<option value={$rows[0]}>{$rows[1]}</option>";
        }
        return $this->table;
    }

    public function listFood() {
        $consult = sprintf($this->food);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        if (!$result) {
            echo 'Error listando los Alimentos \n';
        } while ($rows = pg_fetch_row($result)) {
            echo "<option value={$rows[0]}>{$rows[1]}</option>";
        }
        return $this->table;
    }

    public function listBatch($id) {
        $consult = sprintf($this->bacth, $id);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        if (!$result) {
            echo 'Error listando los Lotes \n';
        } while ($rows = pg_fetch_row($result)) {
            echo "<option value={$rows[0]}>{$rows[1]} - {$rows[3]}</option>";
        }
        return $this->table;
    }

    public function listCattle($batchid) {
        $this->table = "";
        $consult = sprintf($this->cattle, $batchid);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $row = pg_num_rows($result);
        if ($row == 0) {
            $this->table .= "<td>No se Encuentran Ganados para este Lote</td>";
        } else {
            while ($rows = pg_fetch_array($result)) {
                $this->table .= "<tr>";
                $this->table .= "<td>{$rows['cattlecod']}</td>";
                $this->table .= "<td>{$rows['birthdayca']}</td>";
                $this->table .= "<td>{$rows['weightbirth']}</td>";
                $this->table .= "<td>{$rows['weightweaning']}</td>";
                $this->table .= "<td>{$rows['weightact']}</td>";
                $this->table .= "<td>{$rows['ageact']}</td>";
                $this->table .= "<td>{$rows['stage']}</td>";
                $this->table .= "<td>{$rows['dateregister']}</td>";
                $this->table .= "</tr>";
            }
        }
        return $this->table;
    }

    public function listCattleReg($batchid, $d) {
        $this->table = "";
        $consult = sprintf($this->cattle2, $batchid, $d);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $row = pg_num_rows($result);
        if ($row == 0) {
            $this->table .= "<td>No se Encuentran Ganados para este Lote</td>";
        } else {
            while ($rows = pg_fetch_array($result)) {
                $this->table .= "<tr>";
                $this->table .= "<td>{$rows['cattlecod']}</td>";
                $this->table .= "<td>{$rows['birthdayca']}</td>";
                $this->table .= "<td>{$rows['weightbirth']}</td>";
                $this->table .= "<td>{$rows['weightweaning']}</td>";
                $this->table .= "<td>{$rows['ageweaning']}</td>";
                $this->table .= "<td>{$rows['weightact']}</td>";
                $this->table .= "<td>{$rows['ageact']}</td>";
                $this->table .= "<td>{$rows['stage']}</td>";
                $this->table .= "</tr>";
            }
        }
        return $this->table;
    }

    public function listProm() {
        $this->table = "";
        $consult = sprintf($this->prom);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $row = pg_num_rows($result);
        if ($row == 0) {
            $this->table .= "<td>No se Encuentran Promedios para este Lote</td>";
        } else {
            while ($rows = pg_fetch_array($result)) {
                $this->table .= "<tr>";
                $this->table .= "<td>{$rows['batchname']}</td>"; //nombre lote
                $this->table .= "<td>{$rows['namerace']}</td>"; //nombre raza
                $this->table .= "<td>{$rows['namefood']}</td>"; //nombre alimento
                $this->table .= "<td>{$rows['weightbirth']}</td>"; //peso nacim
                $this->table .= "<td>{$rows['weightpreweaning']}</td>"; //peso predestete
                $this->table .= "<td>{$rows['weightweaning']}</td>"; //peso destete
                $this->table .= "<td>{$rows['weightpreceba']}</td>"; //peso preceba
                $this->table .= "<td>{$rows['weightceba']}</td>"; //peso ceba
                $this->table .= "<td>{$rows['agepreweaning']}</td>"; //edad predestete
                $this->table .= "<td>{$rows['ageweaning']}</td>"; //edad destete
                $this->table .= "<td>{$rows['agepreceba']}</td>"; //edad preceba
                $this->table .= "<td>{$rows['ageceba']}</td>"; //edad ceba
                $this->table .= "<td>{$rows['growthdate']}</td>"; //fecha ingreso
                $this->table .= "</tr>";
            }
        }

        return $this->table;	
    }
    
    public function listDisease($diseaseid){
        $this->table = "";
        $consult = sprintf($this->disease, $diseaseid);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $row = pg_num_rows($result);
        if ($row == 0){
            $this->table .= "<td>No se encuentran datos para esta enfermedad</td>";
        } else {
            while ($rows = pg_fetch_array($result)){
                $this->table .= "<tr>";
                $this->table .= "<td>{$rows['diaseasecod']}</td>";
                $this->table .= "</tr>";
                $this->table .= "<tr>";
                $this->table .= "<td>{$rows['diseasename']}</td>";
                $this->table .= "</tr>";
                $this->table .= "<tr>";
                $this->table .= "<td>{$rows['symptoms']}</td>";
                $this->table .= "</tr>";
                $this->table .= "<tr>";
                $this->table .= "<td>{$rows['tratamient']}</td>";
                $this->table .= "</tr>";           
            }
        }
        return $this->table;
    }
    

    public function foodcod($b) {
        $consult = sprintf($this->foodcod, $b);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if ($val == " ") {
            $this->fc = null;
        } else {
            $this->fc = $val;
        }
        return $this->fc;
    }

}

?>
